<template>
	<view>
		<u-navbar title-color="#333" title-bold title-size="36" title="修改手机号" :border-bottom="false"></u-navbar>
		<view class="u-m-t-80 u-m-l-32">
			绑定的手机号：{{infoList.phone?infoList.phone:'*****'}}
		</view>
		<view class="u-m-t-22 u-p-l-32 u-p-r-32">
			<view class="flex">
				<view class="input-div flex w472">
					<view class="input-lable">验证码</view>
					<view class="input-value flex">
						<input type="text" v-model="code" />
					</view>
				</view>
				<view class=" input-div1">
					<u-verification-code :seconds="seconds" ref="uCode1" @change="codeChange1"></u-verification-code>
					<view class="codebtn" @tap="getCode(1)">
						{{tips1}}
					</view>
				</view>
			</view>
			
			<view class="input-div u-m-t-22 flex">
				<view class="input-lable">新手机号</view>
				<view class="input-value">
					<input v-model="newPhone" />
				</view>
			</view>
			
			<view class="flex u-m-t-22">
				<view class="input-div flex w472">
					<view class="input-lable">验证码</view>
					<view class="input-value flex">
						<input type="text" v-model="newcode" />
					</view>
				</view>
				<view class=" input-div1">
					<u-verification-code :seconds="seconds" ref="uCode2" @change="codeChange2"></u-verification-code>
					<view class="codebtn" @tap="getCode(2)">
						{{tips2}}
					</view>
				</view>
			</view>
		</view>
		<view class="btn-bo">
			<button class="btn_login_btn" @click="savephone">确定</button>
		</view>
		
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	export default {
		data() {
			return {
				code: '',
				newPhone: '',
				newcode: '',
				tips1: '',
				tips2: '',
				// refCode: null,
				seconds: 60,
				infoList:[]
			}
		},
		onLoad() {
			// this.userinfo()
		},
		methods: {
			userinfo(){
				this.$http.userinfo({
					token: uni.getStorageSync("token")
				}).then(res => {
					this.infoList = res.msg
				})
			},
			savephone(){
				this.$http.savephone({
					token: uni.getStorageSync("token"),
					"phone": this.newPhone, //新手机号
					"code": this.code, //老手机号验证码
					"new_code": this.newcode //新手机号验证码
				}).then(res => {
					this.$refs.uToast.show({
						title: '修改成功',
					})
					setTimeout(()=>{
						uni.navigateBack()
					},1000)
				})
			},
			codeChange1(text) {
				this.tips1 = text;
			},
			codeChange2(text) {
				this.tips2 = text;
			},
			getCode(e) {
				if(e==1){
					if (this.$refs.uCode1.canGetCode) {
						// 模拟向后端请求验证码
						uni.showLoading({
							title: '正在获取验证码'
						})
						this.$http.sendphonecode({
							token: uni.getStorageSync("token"),
							"mobile": this.infoList.phone //手机号
						}).then(res => {
								uni.hideLoading();
								// 这里此提示会被this.start()方法中的提示覆盖
								this.$u.toast('验证码已发送');
								// 通知验证码组件内部开始倒计时
								this.$refs.uCode1.start();
						})
					} else {
						this.$u.toast('倒计时结束后再发送');
					}
				}
				if(e==2){
					if(!this.$u.test.mobile(this.newPhone)){
						this.$u.toast('请输入正确手机号');
						
						return
					}
					if (this.$refs.uCode2.canGetCode) {
						// 模拟向后端请求验证码
						uni.showLoading({
							title: '正在获取验证码'
						})
						this.$http.sendphonecode({
							token: uni.getStorageSync("token"),
							"mobile": this.newPhone //手机号
						}).then(res => {
								uni.hideLoading();
								// 这里此提示会被this.start()方法中的提示覆盖
								this.$u.toast('验证码已发送');
								// 通知验证码组件内部开始倒计时
								this.$refs.uCode2.start();
						})
					} else {
						this.$u.toast('倒计时结束后再发送');
					}
				}
			},
		}
	}
</script>

<style scoped lang="scss">
	.input-div{
		width: 100%;
		height: 80rpx;
		background: #FFFFFF;
		border-radius: 8rpx 8rpx 8rpx 8rpx;
		border: 1rpx solid #DDDDDD;
		padding: 24rpx 30rpx;
		
		.input-lable{
			font-size: 28rpx;
			color: #000000;
			padding-right: 20rpx;
			width: 140rpx;
		}
		
		.input-value{
			border-left: 1px #BDBDBD solid;
			padding-left: 20rpx;
			width: auto;
		}
	}
	
	.w472{
		width: 472rpx;
	}
	
	.input-div1{
		width: 202rpx;
		height: 82rpx;
		background: #FFFFFF;
		border-radius: 8rpx 8rpx 8rpx 8rpx;
		border: 1rpx solid #DBDBDB;
		font-size: 28rpx;
		color: #E10F21;
		margin-left: 12rpx;
		text-align: center;
		line-height: 82rpx;
	}
	.codebtn{
		font-size: 28rpx;
		color: #E10F21;
	}
	
	.btn-bo{
		width: 750rpx;
		height: 120rpx;
		background: #FFFFFF;
		box-shadow: 0rpx -2rpx 8rpx 2rpx rgba(0,0,0,0.14);
		padding-top: 14rpx;
		position: fixed;
		bottom: 0;
		left: 0;
		
		.btn_login_btn{
			width: 686rpx;
			height: 90rpx;
			background: linear-gradient( 180deg, #FA2D2D 0%, #F40000 100%);
			border-radius: 46rpx 46rpx 46rpx 46rpx;
			// margin-top: 116rpx;
			font-size: 36rpx;
			color: #FFFFFF;
			line-height: 90rpx;
			margin-left: 32rpx;
			text-align: center;
		}
	}
</style>
